#!/usr/bin/python

# ====================
# SharedDefinitions
# ====================

import py_compile
import re


# ===========================================================================
# take a start string and a stop string and a list of file lines
# returns all the lines between the start and stop lines
# including the start and stop lines
# processes only the FIRST occurrence of the block
# ===========================================================================
def getBlock(startString, stopString, stringList):

    '''
    Take a start string and a stop string and a list of file lines
    Returns all the lines between the start and stop lines including the start and stop lines
    Processes only the FIRST occurrence of the block

    Simple case
    >>> getBlock('start', 'stop', ['first line','the start line','middle line','the stop line','last line'])
    ['the start line', 'middle line', 'the stop line']

    Be sure we return only the first block
    >>> getBlock('start', 'stop', ['first','the start line','middle','the stop line','start', 'stop'])
    ['the start line', 'middle', 'the stop line']

    '''

    #print 'getBlock -> entering function\n'
    keep = 0
    block = []
    for x in stringList:
        if startString in x:
            keep = 1
            #print 'getBlock -> starting block\n'
        if keep == 1 and stopString in x:
            block.append(x)
            #print 'getBlock.x = ' + x
            #print 'getBlock -> ending block\n'
            break
        if keep == 1:
            block.append(x)
            #print 'getBlock.x = ' + x + '\n'

    return block            



def getBlockWithStopList(startString, stopList, stringList):
    '''
    take a start string and a stop LIST and a list of file lines
    returns all the lines between the start and stop lines
    including the start and stop lines
    processes only the FIRST occurrence of the block
    will stop once ANY of the strings in the stopList are found
    >>> getBlockWithStopList('start', ['this', 'means', 'stop'], ['start', 'not', 'yet', 'this', 'skipped', 'stop'])	
    ['start', 'not', 'yet', 'this']

    '''
    #print 'getBlock -> entering function\n'
    keep = 0
    block = []
    for x in stringList:
        if startString in x:
            keep = 1

        for y in stopList:
            if keep == 1 and y in x:
                block.append(x)            
                return block
        
        if keep == 1:
            block.append(x)
            
    return block



def stripBlock(startString, stopString, stringList):
    '''
    take a start string and a stop string and a list of file lines
    removes all the lines up to and between the start and stop lines
    including the start and stop lines
    processes only the FIRST occurrence of the block
    returns the original list without the stripped lines

    >>> stripBlock('start', 'stop', ['not', 'saved', 'start', 'skipped', 'stop', 'this', 'is', 'saved'])
    ['this', 'is', 'saved']


    '''

    #print 'getBlock -> entering function\n'    
    startFound = 0
    stopFound = 0
    block = []
    for x in stringList:
        if startString in x:
            startFound = 1
                        
        if startFound == 1 and stopFound == 0 and stopString in x:
            stopFound = 1            
            continue
            
        if startFound == 1 and stopFound == 1:
            block.append(x)
            
    return block            

def stripBlockWithStopList(startString, stopList, stringList):
    '''
    take a start string and a stop list and a list of file lines
    removes all the lines up to and between the start and stop lines
    including the start and stop lines
    processes only the FIRST occurrence of the block
    returns the original list without the stripped lines

    >>> stripBlockWithStopList('start', ['stop', 'now'], ['not', 'saved', 'start', 'skipped', 'stop', 'this', 'is', 'saved'])
    ['stop', 'this', 'is', 'saved']


    '''

    #print 'getBlock -> entering function\n'
    startFound = 0
    stopFound = 0
    block = []
    for x in stringList:
        if startString in x:
            startFound = 1

        if startFound == 1 and stopFound == 0:
            for y in stopList:
                if y in x:
                    stopFound = 1
                    continue

        if startFound == 1 and stopFound == 1:
            block.append(x)

    return block


def getNextLine(stringMark, stringList):
    '''
    given a list of strings return the list member following the one containing the stringMark parameter

    >>> getNextLine('here', ['one', 'two', 'here', 'three', 'four']) 
    'three'

    >>> getNextLine('here', ['one', 'two', 'here'])
    

    >>> getNextLine('here', ['one', 'two', 'three'])


    '''

    mark = 0
    for x in stringList:
        if mark == 1:
            return x
        if stringMark in x:
            mark = 1
            continue
        

def getSubStrFromList(start, stop, s):
    '''
    given a list of strings return the one that contains both the start and the stop string
    but only return what's BETWEEN the start and stop string. 
  
    >>> getSubStrFromList('start', 'stop', ['this is', 'the one', 'start and stop', 'before the last one'])
    'and'

    '''

    for x in s:
        if start in x and stop in x:
            a = x.find(start) + len(start)
            b = x.find(stop,a) 
            c = x[a:b]
            return c.strip()
        
def stripEmptyLines(s):
    '''
    removes empty lines from a list of lines. 
    >>> stripEmptyLines(['   ', 'save', '  ', 'me    ', '    '])
    ['save', 'me    ']
   

    '''

    t = []
    for line in s:
        if len(line.strip()) != 0:
            t.append(line)
    return t

def getBlockReturnString(startStr, stopStr, searchList):
    '''
    accepts a list of strings and return a single string 
    takes a starting string and an ending string and a list of file lines
    returns a single string of all the text between the starting string and
    the ending string, not including the start and end string

    >>> getBlockReturnString('start', 'stop', ['the', 'start line ', 'middle line', 'stop line', 'the last line'])
    'middle line'


    '''

    findList = getBlock(startStr, stopStr, searchList)
    findList = findList[1:-1]     
    findStr = ''.join(findList)    
    return findStr

def getBlockReturnStringWithStopList(startStr, stopList, searchList):
    '''
    takes a starting string and an ending list and a list of file lines
    returns a single string of all the text between the starting string and
    the ending list, not including the start and end string

    >>> getBlockReturnStringWithStopList('start', ['pip', 'end'], ['this is', 'the start line ', ' stop ', 'a pip'])
    ' stop '

    '''

    findList = getBlockWithStopList(startStr, stopList, searchList)
    findList = findList[1:-1]     
    findStr = ''.join(findList)    
    return findStr    
    
def stripHTMLfromList(inList):
    newStr = ''
    newList = []
    for x in inList:
        newStr = stripHTML(x)
        newList.append(newStr)

    return newList


def stripHTML(inString):
    '''
    remove HTML tags from a string
    >>> stripHTML('this <p>is a </q>test<h3>')
    'this is a test'

    '''
    inString = inString.replace('<p>','')
    inString = inString.replace('<P>','')
    
    inString = inString.replace('</p>','\n')
    inString = inString.replace('</P>','\n')

    inString = inString.replace('<q>','')
    inString = inString.replace('<Q>','')

    inString = inString.replace('</q>','')
    inString = inString.replace('</Q>','')

    inString = inString.replace('<h3>','')
    inString = inString.replace('<H3>','')

    inString = inString.replace('</h3>','')
    inString = inString.replace('</H3>','')

    inString = inString.replace('<li>','')
    inString = inString.replace('<LI>','')

    inString = inString.replace('</li>','')
    inString = inString.replace('</LI>','')

    inString = inString.replace('<ul>','')
    inString = inString.replace('<UL>','')

    inString = inString.replace('</ul>','')
    inString = inString.replace('</UL>','')

    inString = inString.replace('<dd>','')
    inString = inString.replace('<DD>','')

    inString = inString.replace('</dd>','')
    inString = inString.replace('</DD>','')

    inString = inString.replace('<td>','')
    inString = inString.replace('<TD>','')

    inString = inString.replace('<tr>','')
    inString = inString.replace('<TR>','')

    inString = inString.replace('</td>','')
    inString = inString.replace('</TD>','')

    inString = inString.replace('</tr>','')
    inString = inString.replace('</TR>','')

    inString = inString.replace('&nbsp;','')

    return inString


def convertLengthStringToInt(lengthStr):
    '''
    Converts a string similar to this "200 KB - 250 KB" to this "200"

    >>> convertLengthStringToInt('200 KB - 240 KB')
    '200'
    
    >>> convertLengthStringToInt('200KB - 240KB')
    '200'

    >>> convertLengthStringToInt('varies')
    '0'


    '''
    strList = re.findall(r'\d+', lengthStr)
    if strList == []: 
        return '0'

    if strList[0] != None:
        return strList[0]
    else:
        return '0'  

py_compile.compile('sharedDefinitions.py')

if __name__ == "__main__":
    import doctest
    doctest.testmod()



